﻿using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.Models;
using IPR_StudyMeetingMrgSys.ViewModel;
using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web;
using System.Web.Http;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    public class EmpRoleController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        BLL.EmpRole Bll = new BLL.EmpRole();
        /// <summary>
        /// 添加模块
        /// </summary>
        /// <param name="model">model.EmpID,model.RoleID</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/EmpRole/Add")]
        public IHttpActionResult Add(Model.EmpRole model)
        {
            log.Info($"EmpID:{model.EmpID};RoleID:{model.RoleID}");

            ResultMsg resultMsg = new ResultMsg();
            //用户已经拥有该角色id  
            if (Bll.Exists(model))
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = "用户已经拥有该角色";
                resultMsg.Data = "";
            }
            else
            {
                Model.EmpRole Mol = new Model.EmpRole();
                Mol.EmpID = model.EmpID;
                Mol.RoleID = model.RoleID;
                int flag= Bll.Add(Mol);
                if (flag > 0)
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = flag;
                }
                else
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error ;
                    resultMsg.Info = "添加失败";
                    resultMsg.Data = flag;
                }
            }
            
            return Json(resultMsg);
        }
       /// <summary>
       /// 得到模块列表
       /// </summary>
       /// <returns></returns>
        [HttpPost]
        [Route("api/EmpRole/Sel")]
        public IHttpActionResult Sel()
        {
            log.Info($"获取用户拥有角色列表");
            ResultMsg resultMsg = new ResultMsg();
            DataTable dt= Bll.GetAllList().Tables[0];
            if (dt.Rows.Count > 0)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = dt;
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "没有获取到对应角色信息";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
        /// <summary>
        /// 删除用户角色信息
        /// </summary>
        /// <param name="model">model.ERID</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/EmpRole/Del")]
        public IHttpActionResult Del(Model.EmpRole model)
        {
            log.Info($"删除用户拥有的角色信息");
            ResultMsg resultMsg = new ResultMsg();
            
            if (Bll.Delete(model.ERID))
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "";
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "删除信息失败";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
       
    }
}
